package app.ui;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import database.local.Business;
import database.local.Deal;
import database.local.Utils;

public class ListTabActivity extends Activity {
	
	ArrayList<Business> list;
	TabHost tabHost;
	ListView lv1;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_tab_layout);
		
		Resources res = getResources(); // Resource object to get Drawables
	    Bundle b = getIntent().getBundleExtra("values");
	    
	    
	    //get lists
	    list = b.getParcelableArrayList("all");
	  //open the list
	  		lv1 = (ListView) findViewById(R.id.list_tab_list_list);
	  		lv1.setAdapter(new BusinessListArrayAdapter(this, list));
	  		lv1.setOnItemClickListener( new AdapterView.OnItemClickListener() {

	  			public void onItemClick(AdapterView<?> parentView, View childView, int position,
	  					long id) {
	  				openDealWindow(list.get(position), list.get(position).getDeals().get(0));
	  			}
	  		});
	  	
	  	}

	  	protected void openDealWindow(Business b, Deal d) {
	  		Intent openDealActivity = new Intent(this, DealViewActivity.class);
	  		Bundle bundle = new Bundle();
	  		
	  		bundle.putParcelable("deal", d);
	  		bundle.putParcelable("business", b);
	  		
	  		openDealActivity.putExtra("values", bundle);
	  		this.startActivity(openDealActivity);
	  		
	  	}
	
		/*-----------------------------------------Menu-----------------------------------------------------*/
		public boolean onCreateOptionsMenu(Menu menu) {
			//super.onCreateOptionsMenu(menu);
			
			MenuInflater inflater = getMenuInflater();
		    inflater.inflate(R.layout.list_menu, menu);
		    return true;
		}
		
		
		
		public boolean onOptionsItemSelected(MenuItem item) {
			//super.onOptionsItemSelected(item);
			
			switch (item.getItemId()){
			case R.id.list_menu_sort :
				openSortDialog();
				return true;
			case R.id.list_menu_map_return :
				
				Intent intent = new Intent();
				setResult(RESULT_CANCELED, intent);
				finish();
				return true;
			}
			
			return false;
		}

		private void openSortDialog() {
			final String[] items = new String[3];
			items[0] = "Rating";
			items[1] = "Distance";
			items[2] = "Name";
			
			new AlertDialog.Builder( this )
	    	.setTitle( "Sort by" )
	    	.setItems(items, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					sortList(items[which]);
				}
	    	}
	    			)
	    	.show();	
		
			
			
			
			
			}
		
		protected void sortList(String string) {
			
			chooseSort(string);
			

			lv1.setAdapter(new BusinessListArrayAdapter(this, list));
			lv1.invalidate();		
		}

		private void chooseSort(String string) {
			if (string.equalsIgnoreCase("Rating")){
				Log.i("Sort", "Sorting by Rating");
				list = (ArrayList<Business>) Utils.getListbyRating(list);
			}
			else if (string.equalsIgnoreCase("Distance")){
				Log.i("Sort", "Sorting by Distance");
				list = (ArrayList<Business>) Utils.getListbyDistance(list);
			}
			else if (string.equalsIgnoreCase("Name")){
				Log.i("Sort", "Sorting by Name");
				list = (ArrayList<Business>) Utils.getListbyName(list);
			}
		}


}
